package eai.tp1;

import java.io.IOException;
import java.util.List;

import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import eai.tp1.core.SSEngine;
import eai.tp1.core.datamodel.DataManager;
import eai.tp1.core.datamodel.DigitalCamera;
import eai.tp1.core.source.DPreviewExtractor;
import eai.tp1.core.source.IWebPageSource;

public class Wrapper {

	public static void main(String args[]) {
		if(args.length != 1)
		{
			System.out.println("ERROR: ARGS != 1");			 
			return;
		}		 
		else {
			DPreviewExtractor extr = new DPreviewExtractor();
			IWebPageSource webSource = extr.factoryMethod();
			SSEngine scraping = new SSEngine();
			DataManager dataManager = new DataManager();
			try {
				List<DigitalCamera> cameras = scraping.extract(
						webSource, args[0]);
				dataManager.write(cameras,args[0], webSource.getRootLink());
				XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
				System.out.println(outputter.outputString(dataManager.getDocument()));
			} catch (IOException e) {
				System.out.println("Error! The brand doesn't exist.");
			}
		}
	}
}
